// import Security from './security'

/**
 * 路由跳转封装
 */
export default class Router {

  /**
   * 生成需要跳转的页面路径，路径后可带参数，形如 path?key=value&key2=value2
   * @param {*} path
   * @param {*} query
   */
  static genUrl(path, query) {
    if (query && typeof query === 'object') {
      const pairs = []
      const holdCharList = [','] // 无需转义的字符

      for (const key in query) {
        const value = query[key]

        // 避免 undefined / false 被处理成字符串
        let pair = `${key}=${encodeURIComponent(value || '')}`

        // 无需转义的字符
        if (value && typeof value === 'string') {
          for (const char of holdCharList) {
            if (value.includes(char)) {
              const reg = new RegExp(`${encodeURIComponent(char)}`, 'g')

              pair = pair.replace(reg, char)
            }
          }
        }

        // ios抖音在参数包含三个‘i’的时候会屏蔽参数，所以加密后规避此规则
        // if (key === 'hm' && path === '/pages/order/haoma') {
        //   pair = `${key}=${encodeURIComponent(Security.encrypt(value || ''))}`
        // }

        pairs.push(pair)
      }
      if (pairs.length > 0) {
        path = path + '?' + pairs.join('&')
      }
    }
    return path
  }
}
